package com.example.day7homework;

import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;

import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity {
    ViewPager vp;
    ImageView im1,im2,im3;
    ArrayList<ImageView> imageAl=new ArrayList<>();
    ArrayList<ImageView> imageShow = new ArrayList<>();
    int aa=0;
    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what==101){
                aa++;
                vp.setCurrentItem(aa);
            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        vp=findViewById(R.id.vp);
        im1=findViewById(R.id.image1);
        im2=findViewById(R.id.image2);
        im3=findViewById(R.id.image3);
        imageAl.add(im1);
        imageAl.add(im2);
        imageAl.add(im3);
        ImageView img1 = new ImageView(this);
        img1.setImageResource(R.mipmap.miao4);
        ImageView img2 = new ImageView(this);
        img1.setImageResource(R.mipmap.miao5);
        ImageView img3 = new ImageView(this);
        img1.setImageResource(R.mipmap.miao6);
        imageShow.add(img1);
        imageShow.add(img2);
        imageShow.add(img3);

        vp.setAdapter(new MyAdapter(this,imageShow));
        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                for(int j=0;j<imageAl.size();j++){
                    imageAl.get(j).setImageResource(R.mipmap.xihuan);
                }
                imageAl.get(i).setImageResource(R.mipmap.xihuan2);
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });

      new Thread(new Runnable() {
          @Override
          public void run() {
              try {
                  Thread.sleep(1000);
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }
          }
      }).start();

        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                if(aa!=imageAl.size()){
                    handler.sendEmptyMessage(101);
                }else{
                    this.cancel();
                }
            }
        },0,1000);
    }
}
